﻿using System;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using BranchOfSansar.Sansar.Enquiry;
using System.IO;
namespace BranchOfSansar.Register.Main
{
    public partial class frmApartmentList : Form
    {
        public sp_GetApartmentList_Result SelectedItem;
        private bool tlstLoaded;
        public frmApartmentList()
        {
            InitializeComponent();
        }
        private void loadData()
        {

            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                #region set Treelist data
                var Dist = obj.DistrictAndQuarterList();

                treeList1.BeginUpdate();
                treeList1.Columns.Add();
                treeList1.Columns[0].Caption = "Дүүрэг, хороо";
                treeList1.Columns[0].VisibleIndex = 0;
                treeList1.EndUpdate();
                treeList1.BeginUnboundLoad();
                TreeListNode parentNode = treeList1.AppendNode(new object[] { "Бүгд" }, null);
                parentNode.Tag = "";
                string distCode = "Бүгд";
                foreach (vw_DistAndQuart d in Dist)
                {
                    if (distCode.Equals(d.DistrictCode))
                    {
                        TreeListNode childNode = treeList1.AppendNode(new object[] { d.QuartName }, parentNode);
                        childNode.Tag = d.QuarterCode;
                    }
                    else
                    {
                        parentNode = treeList1.AppendNode(new object[] { d.DistName }, null);
                        parentNode.Tag = d.DistrictCode;
                        TreeListNode childNode = treeList1.AppendNode(new object[] { d.QuartName }, parentNode);
                        childNode.Tag = d.QuarterCode;
                        distCode = d.DistrictCode;
                    }
                }
                treeList1.EndUnboundLoad();
                tlstLoaded = true;
                #endregion
                #region fill cmbBank
                cmbBank.Properties.DataSource = obj.BankListForSearch();
                cmbBank.Properties.DisplayMember = "Name";
                cmbBank.Properties.ValueMember = "BankID";
                //cmbBank.Properties.BestFitMode = BestFitMode.BestFit;
                //cmbBank.Properties.SearchMode = SearchMode.AutoComplete;                
                cmbBank.ItemIndex = 0;
                #endregion
            }
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                #region fill cmbSection
                //Section[] sss = obj.SectionListForSearch();
                //MessageBox.Show(sss.Count().ToString());
                cmbSection.Properties.DataSource = obj.SectionListForSearch();
                cmbSection.Properties.DisplayMember = "Name";
                cmbSection.Properties.ValueMember = "SectionNo";
                //cmbBank.Properties.BestFitMode = BestFitMode.BestFit;
                //cmbBank.Properties.SearchMode = SearchMode.AutoComplete;                
                cmbSection.ItemIndex = 0;
                #endregion    
                #region fill cmbKontor
                cmbKontor.Properties.DataSource = obj.KontorListForSearch();
                cmbKontor.Properties.DisplayMember = "Name";
                cmbKontor.Properties.ValueMember = "KontorNo";
                //cmbBank.Properties.BestFitMode = BestFitMode.BestFit;
                //cmbBank.Properties.SearchMode = SearchMode.AutoComplete;                
                cmbKontor.ItemIndex = 0;
                #endregion
            }
        }

        private void frmApartmentList_Load(object sender, EventArgs e)
        {
            if (File.Exists(this.Name + ".xml"))
            {
                viewApartment.RestoreLayoutFromXml(this.Name + ".xml");
            }
            loadData();
        }

        private void cmbBank_EditValueChanged(object sender, EventArgs e)
        {
            using (EnquiryClient obj1 = new EnquiryClient(Globals.Token))
            {
                //cmbKass.Properties.DataSource = null;                
                cmbKass.Properties.DataSource = obj1.BankBranchesForSearch(Convert.ToInt32(cmbBank.EditValue));
                cmbKass.Properties.DisplayMember = "Name";
                cmbKass.Properties.ValueMember = "BranchID";
                //cmbBank.Properties.BestFitMode = BestFitMode.BestFit;
                //cmbBank.Properties.SearchMode = SearchMode.AutoComplete;                
                cmbKass.ItemIndex = 0;
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            loadGrid();
        }
        private void loadGrid()
        { 
            string Dist=string.Empty;
            string Quart=string.Empty;
            if(treeList1.Selection[0].ParentNode==null)
            {
                Dist=treeList1.Selection[0].Tag.ToString();
            }
            else
            {
                Quart=treeList1.Selection[0].Tag.ToString();
            }

            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                gridControl1.DataSource = obj.ApartmentList(Dist, Quart, Convert.ToInt32(cmbBank.EditValue.ToString()), Convert.ToInt32(cmbKass.EditValue), Convert.ToInt32(cmbSection.EditValue), Convert.ToInt32(cmbKontor.EditValue), txtName.Text, txtKod.Text, txtDugaar.Text, txtParent.Text);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmApartment frm = new frmApartment();
            frm.Tag = "new";
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewApartment.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
                        
            frmApartment frm = new frmApartment();
            sp_GetApartmentList_Result apart = (sp_GetApartmentList_Result)viewApartment.GetRow(viewApartment.GetSelectedRows()[0]);
            frm.Tag = apart.Id;
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (viewApartment.SelectedRowsCount > 0)
            {
                SelectedItem = (sp_GetApartmentList_Result)viewApartment.GetRow(viewApartment.GetSelectedRows()[0]);
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tlstLoaded == false) return;
            if (treeList1.Selection[0].ParentNode == null)
            {
                if (treeList1.Selection[0].Tag.ToString().Length>0 )
                {
                    loadGrid();
                }

            }
            else
            {
                loadGrid();
            }
        }

        private void frmApartmentList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                loadGrid();
            }
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                saveFileDialog1.Title = "Specify Destination Filename";
                saveFileDialog1.Filter = "excell Files|*.xls";
                saveFileDialog1.FilterIndex = 1;
                saveFileDialog1.OverwritePrompt = true;
                saveFileDialog1.CreatePrompt = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK & !string.IsNullOrEmpty(saveFileDialog1.FileName))
                {

                    gridControl1.ExportToExcelOld(saveFileDialog1.FileName);
                }
            }
            catch (Exception ex)
            {
                Globals.Err(ex.Message);
            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void frmApartmentList_FormClosing(object sender, FormClosingEventArgs e)
        {
            viewApartment.SaveLayoutToXml(this.Name + ".xml");
        }
    }
}
